package com.community.controller.room;

import com.community.entity.ContactRoom;
import com.community.entity.Result;
import com.community.entity.Room;
import com.community.service.room.RoomService;
import com.community.utils.JWT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.community.utils.JWT.getUserId;

@Slf4j
@Controller
public class RoomController {

    @Autowired
    private RoomService roomService;

    @ResponseBody
    @PostMapping("/room")
    public Result operateRoom(@RequestBody Room r, HttpServletRequest request){

        String userId = getUserId(request);

        String id = r.getId();
        String point = r.getPoint();
        String building = r.getBuilding();
        String name = r.getName();
        String room = r.getRoom();
        String gender = r.getGender();
        String mobile = r.getMobile();

        log.info("id:{}", id);
        log.info("userId:{}", userId);
        log.info("name:{}", name);
        log.info("gender:{}", gender);
        if (id == null || id.isEmpty())  //添加
        {
            id = UUID.randomUUID().toString();
            roomService.addRoom(id, point, building, name, room, gender, mobile, userId);
        }else {  //修改
            roomService.editRoom(id, point, building, name, room,  gender, mobile);
        }

        Map<String,String> data = new HashMap<>();
        data.put("id",id);
        return Result.success(data);
    }


    @ResponseBody
    @GetMapping("/room")
    public Result getRoomInfo(HttpServletRequest request){
        String userId = getUserId(request);
        log.info("用户的id为:{}", userId);
        List<Room> list = roomService.getRoomInfo(userId);

        return Result.success(list);
    }


    @ResponseBody
    @DeleteMapping("/room/{id}")
    public Result deleteRoom(@PathVariable("id") String id){
        roomService.deleteRoom(id);
        log.info("id的值为:{}", id);
        Map<String, String> data = new HashMap<>();
        data.put("id", id);
        return Result.success(data);
    }


    @ResponseBody
    @GetMapping("/room/{id}")
    public Result getRoomDetail(@PathVariable("id") String id){
        Room room = roomService.getRoomDetail(id);

        return Result.success(room);
    }

    @ResponseBody
    @GetMapping("/house")
    public Result getPassHouse(HttpServletRequest request){

        String id = getUserId(request);
        log.info("该用户的id为:{}", id);

        List<ContactRoom> list = roomService.getStatusInfo(id);

        return Result.success(list);
    }

}
